package com.kpx.app.wigdet;

import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kpx.app.R;

/**
 * 释放对话框消失权限,交给用户,用法同系统Dialog
 *
 * @author scott
 */
public class GreenDialog extends BaseDialog implements View.OnClickListener {
    private TextView mNegativeBtn;
    private TextView mPositiveBtn;
    private TextView txtContent;
    private TextView txt_title;
    private LinearLayout mButtonLayout;

    private DialogInterface.OnClickListener onClickListener;

    public GreenDialog(Context context) {
        super(context);
        setContentView(R.layout.dialog_green);
        mNegativeBtn = (TextView) findViewById(R.id.btn_negative);
        mPositiveBtn = (TextView) findViewById(R.id.btn_positive);
        txt_title = (TextView) findViewById(R.id.txt_title);
        txtContent = (TextView) findViewById(R.id.txt_content);
        mButtonLayout = (LinearLayout) findViewById(R.id.layout_button);
        mNegativeBtn.setOnClickListener(this);
        mPositiveBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (null != onClickListener) {
            int which = v.getId() == R.id.btn_negative ? DialogInterface.BUTTON_NEGATIVE : BUTTON_POSITIVE;
            onClickListener.onClick(this, which);
            // 点击Negative按钮,关闭对话框
            if (R.id.btn_negative == v.getId()) dismiss();
        }
    }

    public void setOnClickListener(DialogInterface.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setTitle(String title) {
        txt_title.setVisibility(View.VISIBLE);
        txt_title.setText(title);
    }

    public void setContent(String content) {
        txtContent.setText(content);
    }

    public void setTextSize(float textSize) {
        txtContent.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
        mPositiveBtn.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
        mNegativeBtn.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
    }

    public void setPositiveButtonText(String text) {
        mPositiveBtn.setText(text);
        if (!TextUtils.isEmpty(text)) {
            mButtonLayout.setVisibility(View.VISIBLE);
            mPositiveBtn.setVisibility(View.VISIBLE);
        } else {
            if (TextUtils.isEmpty(mNegativeBtn.getText())) {
                mButtonLayout.setVisibility(View.GONE);
            }
        }
    }

    public void setNegativeButtonText(String text) {
        mNegativeBtn.setText(text);
        if (!TextUtils.isEmpty(text)) {
            mButtonLayout.setVisibility(View.VISIBLE);
            mNegativeBtn.setVisibility(View.VISIBLE);
        } else {
            if (TextUtils.isEmpty(mPositiveBtn.getText())) {
                mButtonLayout.setVisibility(View.GONE);
            }
        }
    }

    public static class Builder {
        private Context mContext;
        private String title;
        private String content;
        private String mPositiveButtonText;
        private String mNegativeButtonText;
        private float textSize = -1;
        private DialogInterface.OnClickListener onClickListener;
        private boolean cancelable = true;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContent(String text) {
            content = text;
            return this;
        }

        public Builder cancelable(boolean cancelable) {
            this.cancelable = cancelable;
            return this;
        }

        public Builder setPositiveButton(String text) {
            mPositiveButtonText = text;
            return this;
        }

        public Builder setNegativeButton(String text) {
            mNegativeButtonText = text;
            return this;
        }

        public Builder setTextSize(float size) {
            textSize = size;
            return this;
        }

        public Builder onClickListener(DialogInterface.OnClickListener onClickListener) {
            this.onClickListener = onClickListener;
            return this;
        }

        public GreenDialog create() {
            GreenDialog greenDialog = new GreenDialog(mContext);
            if (!TextUtils.isEmpty(mPositiveButtonText)) {
                greenDialog.mPositiveBtn.setText(mPositiveButtonText);
                greenDialog.mPositiveBtn.setVisibility(View.VISIBLE);
            } else {
                greenDialog.mPositiveBtn.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(mNegativeButtonText)) {
                greenDialog.mNegativeBtn.setText(mNegativeButtonText);
                greenDialog.mNegativeBtn.setVisibility(View.VISIBLE);
            } else {
                greenDialog.mNegativeBtn.setVisibility(View.GONE);
            }
            if (TextUtils.isEmpty(mPositiveButtonText) && TextUtils.isEmpty(mNegativeButtonText)) {
                greenDialog.mButtonLayout.setVisibility(View.GONE);
            } else {
                greenDialog.mButtonLayout.setVisibility(View.VISIBLE);
            }
            if (!TextUtils.isEmpty(title)) {
                greenDialog.setTitle(title);
            }
            if (!TextUtils.isEmpty(content)) {
                greenDialog.txtContent.setText(content);
            }
            if (textSize > 0) {
                greenDialog.txtContent.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
                greenDialog.mPositiveBtn.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
                greenDialog.mNegativeBtn.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
            }
            if (null != onClickListener) {
                greenDialog.setOnClickListener(onClickListener);
            }
            greenDialog.setCanceledOnTouchOutside(cancelable);
            greenDialog.setCancelable(cancelable);
            return greenDialog;
        }
    }
}
